<template>
  <el-container :class="{ 'home-page': $route.path === '/' }">
    <el-header height="60" v-if="!isLoginPage">
      <router-link to="/" class="logo">
        知识图谱管理系统
      </router-link>
      <div class="nav-menu" v-if="isLoggedIn">
        <router-link to="/" v-if="$route.path !== '/'">
          <el-button>首页</el-button>
        </router-link>
        <router-link to="/document" v-if="isAdmin">
          <el-button>知识抽取</el-button>
        </router-link>
        <router-link to="/knowledge">
          <el-button>知识查询</el-button>
        </router-link>
        <router-link to="/graph">
          <el-button>图谱可视化</el-button>
        </router-link>
      </div>
      <div class="user-menu" v-if="isLoggedIn">
        <el-dropdown trigger="click" @command="handleCommand">
          <span class="el-dropdown-link">
            {{ username }}
            <el-icon class="el-icon--right"><arrow-down /></el-icon>
          </span>
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item command="logout">退出登录</el-dropdown-item>
            </el-dropdown-menu>
            <el-dropdown-menu>
              <el-dropdown-item command="my-page">个人主页</el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </div>
    </el-header>
    <el-main>
      <router-view></router-view>
    </el-main>
  </el-container>
</template>

<script setup>
import { ref, computed } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { ArrowDown } from '@element-plus/icons-vue'

const router = useRouter()
const route = useRoute()
const username = computed(() => localStorage.getItem('username') || '用户')
const isAdmin = computed(() => localStorage.getItem('isAdmin') === 'true')
const isLoggedIn = computed(() => !!localStorage.getItem('token'))
const isLoginPage = computed(() => ['/login', '/register'].includes(route.path))

const handleCommand = (command) => {
  if (command === 'logout') {
    localStorage.removeItem('token')
    localStorage.removeItem('username')
    localStorage.removeItem('isAdmin')
    router.push('/login')
  }
  if (command === 'my-page') {
    router.push('/my-page'); // 跳转到个人主页
  }
}
</script>

<style>
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

html, body, #app {
  height: 100vh;
  width: 100vw;
  margin: 0;
  padding: 0;
  overflow-x: hidden;
  font-family: 'Helvetica Neue', Helvetica, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', '微软雅黑', Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  background-color: var(--el-bg-color);
  color: var(--el-text-color-primary);
}

.el-container {
  min-height: 100vh;
  width: 100%;
  display: flex;
}

.el-header {
  height: 60px;
  background-color: var(--el-bg-color);
  border-bottom: 1px solid var(--el-border-color-light);
  padding: 0 20px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 100;
  box-shadow: var(--el-box-shadow-light);
}

.logo {
  display: flex;
  align-items: center;
  font-size: 20px;
  font-weight: 600;
  color: var(--el-color-primary);
  text-decoration: none;
  transition: color 0.3s;
}

.logo:hover {
  color: var(--el-color-primary-light-3);
}

.nav-menu {
  display: flex;
  align-items: center;
  gap: 16px;
}

.nav-menu .el-button {
  padding: 8px 16px;
  font-weight: 500;
}

.nav-menu .router-link-active .el-button {
  background-color: var(--el-color-primary-light-9);
  color: var(--el-color-primary);
}

.user-menu {
  display: flex;
  align-items: center;
  gap: 12px;
}

.el-dropdown-link {
  display: flex;
  align-items: center;
  gap: 4px;
  cursor: pointer;
  color: var(--el-text-color-regular);
  font-size: 14px;
  transition: color 0.3s;
}

.el-dropdown-link:hover {
  color: var(--el-color-primary);
}

.el-main {
  flex: 1;
  padding: 0;
  margin-top: 60px;
  min-height: calc(100vh - 60px);
  width: 100%;
  position: relative;
  background-color: var(--el-bg-color-page);
}

@media screen and (max-width: 768px) {
  .nav-menu {
    gap: 8px;
  }
  
  .nav-menu .el-button {
    padding: 6px 12px;
    font-size: 13px;
  }
  
  .logo {
    font-size: 16px;
  }
}

@media screen and (max-width: 576px) {
  .el-header {
    padding: 0 12px;
  }
  
  .nav-menu {
    gap: 4px;
  }
  
  .nav-menu .el-button {
    padding: 4px 8px;
    font-size: 12px;
  }
}
</style>
